# encoding: utf-8
class Report::CareAndMaintenanceReportController < ApplicationController

  def show
    @title_view ='Reporte de Cuidado y Mantenimientos'
    @heritages = CulturalHeritage::CulturalHeritage.all
    @institutions = CulturalHeritage::CulturalInstitution.all
    @types = CulturalHeritage::CareAndMaintenanceType.all
  end

  def show_pdf
    @title_view ='Reporte de Cuidado y Mantenimientos'
    @heritage_name = params[:heritage_name]
    @institution_name = params[:institution_name]
    @care_type = params[:care_type]
    @cultural_heritages_care = CulturalHeritage::CareAndMaintenance.all

    if ! @heritage_name.blank?
      if ! @institution_name.blank?
        if ! @care_type.blank?
          @cultural_heritages_care = CulturalHeritage::CareAndMaintenance.find(:all,:conditions=>["cultural_heritage_care_and_maintenances.cultural_heritage = ? and cultural_heritage_care_and_maintenances.cultural_institution = ? and cultural_heritage_care_and_maintenances.care_and_maintenance_type = ?",@heritage_name,@institution_name,@care_type])
        else
          @cultural_heritages_care = CulturalHeritage::CareAndMaintenance.find(:all,:conditions=>["cultural_heritage_care_and_maintenances.cultural_heritage = ? and cultural_heritage_care_and_maintenances.cultural_institution = ?",@heritage_name,@institution_name])
        end
      else
        if ! @care_type.blank?
          @cultural_heritages_care = CulturalHeritage::CareAndMaintenance.find(:all,:conditions=>["cultural_heritage_care_and_maintenances.cultural_heritage = ? and cultural_heritage_care_and_maintenances.care_and_maintenance_type = ?",@heritage_name,@care_type])
        else
          @cultural_heritages_care = CulturalHeritage::CareAndMaintenance.find(:all,:conditions=>["cultural_heritage_care_and_maintenances.cultural_heritage = ?",@heritage_name])
        end
      end
    else
      if ! @institution_name.blank?
        if ! @care_type.blank?
          @cultural_heritages_care = CulturalHeritage::CareAndMaintenance.find(:all,:conditions=>["cultural_heritage_care_and_maintenances.cultural_institution = ? and cultural_heritage_care_and_maintenances.care_and_maintenance_type = ?",@institution_name,@care_type])
        else
          @cultural_heritages_care = CulturalHeritage::CareAndMaintenance.find(:all,:conditions=>["cultural_heritage_care_and_maintenances.cultural_institution = ?",@institution_name])
        end
      else
        if ! @care_type.blank?
          @cultural_heritages_care = CulturalHeritage::CareAndMaintenance.find(:all,:conditions=>["cultural_heritage_care_and_maintenances.care_and_maintenance_type = ?",@care_type])
        end
      end
    end

    pdf = Prawn::Document.new

    pdf.image "#{RAILS_ROOT}/public/images/pukara_logo.png", :width => 80, :at => [0, 715]
    pdf.text_box "Sistema de información cultural pukara",
      :at => [100, 715],
      :width => 430
    pdf.text_box "Reporte de Cuidados y Mantenimientos",
      :at => [100, 690],
      :width => 430,
      :style => :bold,
      :size => 20

    pdf.move_down 150

    arr = Array.new

    #Colocando headers
    arr[0] = Array.new
    arr[0][0] = "ID"
    arr[0][1] = "Encargado"
    arr[0][2] = "Fecha Inicio"
    arr[0][3] = "Fecha Fin"
    arr[0][4] = "Institución"
    arr[0][5] = "Patrimonio Cultural"
    arr[0][6] = "Tipo"
    arr[0][7] = "Magn."
    arr[0][8] = "Aprob."

    j = 1

    @cultural_heritages_care.each {
      |care|

      arr[j] = Array.new
      arr[j][0] = care.id
      arr[j][1] = care.obj_security_people.name + " " + care.obj_security_people.lastname
      arr[j][2] = care.start_date.to_s
      arr[j][3] = care.end_date.to_s
      arr[j][4] = care.obj_cultural_institution.name
      arr[j][5] = care.obj_cultural_heritage.name
      arr[j][6] = care.obj_care_and_maintenance_type.name
      arr[j][7] = care.str_mayor
      arr[j][8] = care.str_proceed
      j = j + 1
    }

    pdf.table arr, :column_widths => [25, 75, 60, 60, 75, 75, 65, 50, 55], :row_colors => ["F0F0F0", "FFFFFF"], :header => true do
      row(0).border_width = 2
      row(0).font_style = :bold
    end

    #Pie de pagina
    pdf.repeat(:all, :dynamic => true) do
      pdf.draw_text Time.now.strftime("%d-%m-%Y").to_s, :at => [0, 0]
      pdf.draw_text pdf.page_number, :at => [530, 0]
    end

    send_data pdf.render, :filename => "Reporte de Cuidado y Mantenimientos" << ".pdf", :type => "application/pdf"

  end

  def pdf_export

    cultural_heritage_care = CulturalHeritage::CareAndMaintenance.find(params[:id])

    pdf = Prawn::Document.new

    pdf.image "#{RAILS_ROOT}/public/images/pukara_logo.png", :width => 80, :at => [0, 715]
    pdf.text_box "Sistema de información cultural pukara",
      :at => [100, 715],
      :width => 430
    pdf.text_box "Cuidado y Mantenimiento N° " << params[:id],
      :at => [100, 690],
      :width => 430,
      :style => :bold,
      :size => 20
#    pdf.text_box params[:id],
#      :at => [100, 655],
#      :width => 430,
#      :style => :bold,
#      :size => 20

    #IMAGEN

    pdf.move_down 160

    pdf.text "<b>Código:</b> " << params[:id],
      :inline_format => true

    pdf.move_down 20

    pdf.text "<b>Encargado:</b> " << cultural_heritage_care.obj_security_people.name + " " + cultural_heritage_care.obj_security_people.lastname,
      :inline_format => true

    pdf.move_down 20

    pdf.text "<b>Descripción:</b> " << cultural_heritage_care.description,
      :inline_format => true

    pdf.move_down 20

    pdf.text "<b>Fecha Inicio:</b> " << cultural_heritage_care.start_date.to_s,
      :inline_format => true

    pdf.move_down 20

    pdf.text "<b>Fecha Fin:</b> " << cultural_heritage_care.end_date.to_s,
      :inline_format => true

    pdf.move_down 20
    if cultural_heritage_care.obj_cultural_heritage.typeCulturalInstitution != 2
    pdf.text "<b>Institución Cultural:</b> " << cultural_heritage_care.obj_cultural_heritage.obj_culturalInstitution.name,
      :inline_format => true
    end
    pdf.move_down 20

    pdf.text "<b>Patrimonio Cultural:</b> " << cultural_heritage_care.obj_cultural_heritage.name,
      :inline_format => true

    pdf.move_down 20

    pdf.text "<b>Tipo:</b> " << cultural_heritage_care.obj_care_and_maintenance_type.name,
      :inline_format => true

    pdf.move_down 20

    pdf.text "<b>Magnitud:</b> " << cultural_heritage_care.str_mayor,
      :inline_format => true

    pdf.move_down 20

    if  cultural_heritage_care.str_mayor == "Mayor"
    pdf.text "<b>Supervisor:</b> " << cultural_heritage_care.obj_care_and_maintenance_supervisor.name + " " + cultural_heritage_care.obj_care_and_maintenance_supervisor.lastname,
      :inline_format => true
    pdf.move_down 20
    end

    pdf.text "<b>Aprobación:</b> " << cultural_heritage_care.str_proceed,
      :inline_format => true



    pdf.repeat(:all, :dynamic => true) do
      pdf.draw_text Time.now.strftime("%d-%m-%Y").to_s, :at => [0, 0]
      pdf.draw_text pdf.page_number, :at => [530, 0]
    end


    send_data pdf.render, :filename => "Cuidado y Mantenimiento " << params[:id] << ".pdf", :type => "application/pdf"
  end
end
